﻿using MergeExcel.Models;

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace MergeExcel.Common {
    public static class Comm {
        public static readonly DataTable TableA;
        public static readonly DataTable TableB;
        public static ConfigModel Cfg;
        public static Action<int, int> SetProgress;
        public static Action ProgressGo;
        public static Action<string> SetMsg;
        public static Action<bool> EF;
        public static string UpdateServer;
        public static Func<string> OutputFileName;
        private static readonly string configPath;
        private static readonly string updateServerFilePath;
        static Comm()
        {
            TableA = new DataTable();
            TableB = new DataTable();
            configPath = Path.Combine(Application.StartupPath, "config.set");
            updateServerFilePath = Path.Combine(Application.StartupPath, "updateserver.txt");
            Cfg = new ConfigModel
            {
                SameCfg = new SameConfigModel(),
                DiffCfg = new DiffConfigModel(),
                MergeCfg = new MergeConfigModel()
            };
            GetConfig();
        } 
        
        #region 读取配置
        public static void GetConfig()
        {
            if(!File.Exists(configPath))
            {
                SaveConfig();
            }
            else
            {
                string json = File.ReadAllText(configPath);
                if (!json.Empty())
                    Cfg = JsonConvert.DeserializeObject<ConfigModel>(json);
            }
        }
        #endregion
        #region 保存配置
        public static void SaveConfig()
        {
            string json = JsonConvert.SerializeObject(Cfg);
            File.WriteAllText(configPath, json);
        }
        #endregion
        #region 读取更新服务器地址
        public static void GetUpdateServer()
        {
            if(!File.Exists(updateServerFilePath))
            {
                var lines = File.ReadAllLines(updateServerFilePath)?.ToList();
                UpdateServer = lines.FirstOrDefault();
            }
        }
        #endregion

    }
}
